package com.hf.excel.handler;

import org.apache.poi.xssf.model.SharedStringsTable;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hf
 * @Date 2019-05-16
 * @Time 09:26
 **/
public class MySheetHandler extends DefaultHandler {
    /**
     * 所有共享这个wordbook中字符串集合
     */
    private SharedStringsTable sst;

    /**
     * 单元格内容
     */
    private String lastContents;
    /**
     * 是不是字符串
     */
    private boolean nextIsString;

    /**
     * 当前行
     */
    private int currentRowNum;

    /**
     * 当前列数
     */
    private int currentCellNum;

    /**
     * 开始行
     */
    private int startRowNum;

    /**
     * 结束列
     */
    private int endCellNum;

    /**
     * 结果集
     */
    private List<List<String>> datas;

    /**
     * 每一行数据
     */
    private List<String> data;

    private boolean needParse = true;

    /**
     * 每行空值计数
     */
    private int nullCellNum;


    public MySheetHandler(SharedStringsTable sst, int startRowNum, int endCellNum, List<List<String>> datas) {
        this.sst = sst;
        this.startRowNum = startRowNum -1;
        this.endCellNum = endCellNum;
        this.datas = datas;
    }


    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) {
        if (!needParse){
            return;
        }
        if (currentRowNum < startRowNum || currentCellNum >= endCellNum){
            return;
        }
        if ("row".equals(name)){
            data = new ArrayList<>(endCellNum);
        }
        //c => cell
        if("c".equals(name)) {
            String cellType = attributes.getValue("t");
            nextIsString = "s".equals(cellType);
        }
        // 清楚缓存内容
        lastContents = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        if (!needParse){
            return;
        }
        if (currentRowNum < startRowNum || currentCellNum >= endCellNum){
            return;
        }
        lastContents += new String(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name) {
        if (!needParse){
            return;
        }
        if (currentCellNum > endCellNum){
            currentRowNum ++;
            if (data != null){
                datas.add(data);
            }
            currentCellNum = 0;
            nullCellNum = 0;
            return;
        }
        if ("row".equals(name)){
            currentRowNum ++;
            if (data != null){
                datas.add(data);
            }
            currentCellNum = 0;
            nullCellNum = 0;
            return;
        }
        if (currentRowNum < startRowNum){
            return;
        }
        if(nextIsString) {
            int idx = Integer.valueOf(lastContents);
            lastContents = sst.getItemAt(idx).getString();
            nextIsString = false;
        }
        if ("c".equals(name)){
            currentCellNum ++;
            if (lastContents == null || "".equals(lastContents.trim())){
                nullCellNum ++ ;
                data.add(lastContents);
            }
        }
        if (nullCellNum == endCellNum){
            needParse = false;
        }
        // v => contents of a cell
        if("v".equals(name)) {
            data.add(lastContents);
        }
    }

}
